const state = {
  // admin: {
  //   id: 0,
  //   avatar: "http://182.61.31.180:5000/images/1653403893152_logo.png",
  //   name: "系统消息",
  //   newMessage: "欢迎使用书多多聊天功能，请问有什么可以帮助您的吗？",
  //   time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
  //   unreadNum: 1,
  // },
  curChat: {},
  chatList: [
    {
      id: 2,
      avatar: "http://182.61.31.180:5000/images/1653403893152_logo.png",
      name: "管理员",
      newMessage: "你好，我是管理员，请问有什么可以帮助您的吗？",
      messageList: [
        {
          id: 1,
          type: "my",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "http://192.168.31.63:3066/Files/1656254744583_1656254745248_1656207725727.jpeg",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "my",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "你好，我是管理员，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
      ],
      time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
      unreadNum: 1,
    },
    {
      id: 3,
      avatar: "http://182.61.31.180:5000/images/1653403893152_logo.png",
      name: "测试用户",
      newMessage: "你好，我是测试用户，请问有什么可以帮助您的吗？",
      time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
      unreadNum: 1,
      messageList: [
        {
          id: 1,
          type: "my",
          content: "你好，我是测试用户，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
        {
          id: 2,
          type: "other",
          content: "你好，我是测试用户，请问有什么可以帮助您的吗？",
          time: "Thu Jun 02 2021 00:17:37 GMT+0800 (中国标准时间)",
        },
      ],
    },
  ],
  unreadNum: 0,
};
const mutations = {
  SET_CURRENT_CHAT(state, index) {
    state.curChat = state.chatList[index];
  },
  SEND_MSG(state, msg) {
    state.curChat.messageList.push(msg);
  },
  // 添加新消息
  ADD_NEW_MSG(state, msg) {
    state.chatList.push(msg);
  },
  // 更新消息列表
  UPDATE_MSG_LIST(state, msg) {
    state.chatList.unshift(msg);
  }
};
const actions = {
  addUserMsg(context, msg) {
    // 查看当前聊天列表中是否有该用户，如果有，则更新该用户的消息列表，如果没有，则添加一个新的用户到聊天列表中
    let user = context.state.chatList.find(item => item.id === msg.senderId);
    console.log('user',user)
    if (user) {
      context.commit('UPDATE_MSG_LIST',msg)
    } else {
      let msgData={
        id: msg.senderId,
        avatar: msg.senderAvatar,
        name: msg.senderName,
        newMessage: msg.content,
        time: msg.time,
        messageList:[msg],
        unreadNum: 1
      }
      context.commit("ADD_NEW_MSG", msgData);
    }
    
  },
  sendMsg({commit}, msg) {
    commit("SEND_MSG", msg);
  }
};
const getters = {
  curChat(state) {
    return state.curChat;
  },
  chatList(state) {
    return state.chatList;
  },
  // admin(state) {
  //   return state.admin;
  // },
};
export default {
  state,
  mutations,
  actions,
  getters,
};
